package com.example.administrator.myproject;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.administrator.myproject.ButtonFragment.FindFragment;
import com.example.administrator.myproject.ButtonFragment.MyFragment;
import com.example.administrator.myproject.ButtonFragment.TujianFragment;
import com.example.administrator.myproject.ButtonFragment.YanchuFragment;

public class MainActivity extends AppCompatActivity {

    private RadioGroup radioGroup;
    private ViewPager mVp;
    private Toolbar toolBar;
    private FragmentManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioGroup.check(R.id.bt_01);
        int childCount = radioGroup.getChildCount();
        RadioButton[] rabtns = new RadioButton[childCount];
        Log.i("TAG","===data");

        // FragmentManager管理器
        manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        TujianFragment tujianfragment = new TujianFragment();

        transaction.add(R.id.ll, tujianfragment);
        transaction.commit();
        //默认显示第一个Button
        radioGroup.check(R.id.bt_01);
        //RadioGroup与Fragment联动
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                FragmentTransaction transaction1 = manager.beginTransaction();
                TujianFragment tujianfragment = new TujianFragment();
                YanchuFragment yanchuFragment = new YanchuFragment();
                FindFragment findFragment = new FindFragment();
                MyFragment myFragment = new MyFragment();
                switch (i) {
                    case R.id.bt_01:
                        transaction1.replace(R.id.ll, tujianfragment);
                        transaction1.commit();
                        break;
                    case R.id.bt_02:
                        transaction1.replace(R.id.ll, yanchuFragment);
                        transaction1.commit();
                        break;
                    case R.id.bt_03:
                        transaction1.replace(R.id.ll, findFragment);
                        transaction1.commit();
                        break;
                    case R.id.bt_04:
                        transaction1.replace(R.id.ll, myFragment);
                        transaction1.commit();
                        break;



                }

            }
        });


    }

}
